//
//  MusicViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MusicViewController.h"
#import <AFNetworking.h>
#import "AsiaNewSongListModel.h"
#import "ChanelModel.h"
#import "ExclusiveRegionModel.h"
#import "ExclusuveAritstModel.h"
#import "HotListSongModel.h"
#import "NewSongModel.h"
#import "PosterModel.h"
#import "RecomendListModel.h"
#import "RecomendSongModel.h"
#import "JHJTableViewCell.h"
#import "ExclusiveRegionTableViewCell.h"
#import "RecommendSongsTableViewCell.h"
#import "MyScrollView.h"
#import "PosterModel.h"
#import "PosterDetailViewController.h"
#import "PosterDetailWebViewController.h"
#import "NewSongNewDVDViewController.h"
#import "HotListViewController.h"
#import "RecommendViewController.h"
#import "RecommendListViewController.h"
#import "ExclusiveRegionViewController.h"
#import "ExclusiveArtistViewController.h"
#import "SongListDetailViewController.h"
#import "MJRefresh.h"



//static NSString *const newSongCell = @"newSongCell";

@interface MusicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray *musicArray;
//@property (nonatomic, strong)NSArray *musicModelsArr;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *scrollArray;
@property (nonatomic, strong)MyScrollView *myView;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;

@end

@implementation MusicViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 1;
    self.automaticallyAdjustsScrollViewInsets = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.tabBar showTabBarWithAnimation:NO];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollArray = [NSMutableArray array];
    self.musicArray = [NSMutableArray array];
    
    // Do any additional setup after loading the view.
    [self createData];
    [self createView];
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0x87CEFA, 0x668B8B);
//    [self addHeader];
}

- (void)addHeader {
    __block MusicViewController *musicVC = self;
    [self.tableView addHeaderWithCallback:^{
        [musicVC createData];
        
    }];
}

- (void)createData {
    NSLog(@"333");
    self.scrollArray = [NSMutableArray array];
    self.musicArray = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1463057210320&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        int i = 0;
        for (NSDictionary *dic in [responseObject valueForKey:@"data"]) {
            
            MusicModel *model = [[MusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            if (i < 2) {
                [self.scrollArray addObject:model];
                i++;
            } else {
                [self.musicArray addObject:model];
            }
        }
        PosterModel *posterModel = self.scrollArray[0];
        [self.myView setImages:posterModel.data];
        [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.tableView registerClass:[JHJTableViewCell class] forCellReuseIdentifier:NSStringFromClass([JHJTableViewCell class])];
    [self.tableView registerClass:[ExclusiveRegionTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ExclusiveRegionTableViewCell class])];
    [self.tableView registerClass:[RecommendSongsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RecommendSongsTableViewCell class])];
    self.tableView.separatorStyle = 0;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    self.myView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 200)];
    [view addSubview:self.myView];
    MusicViewController *myMusicVC = self;
    self.myView.scrollBlock = ^(DataModel * model) {
        //写跳转方法;
        [myMusicVC pushPosterViewControllerWithModel:model];
    };
    self.tableView.tableHeaderView = view;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"theme_defaultskin"];
//    imageView.dk_imagePicker =DKImagePickerWithNames(@"theme_defaultskin", @"global_bg");
    self.tableView.backgroundView = imageView;
    UIImage *image1 = [UIImage imageNamed:@"theme_defaultskin"];
    UIImage *image2 = [UIImage imageNamed:@"global_bg"];
    imageView.dk_imagePicker = [DKImage pickerWithNormalImage:image1    nightImage:image2];
//    DKImagePickerWithNames(@"theme_defaultskin", @"global_bg");
//    NSInvocation
}

- (void)pushPosterViewControllerWithModel:(DataModel *)model {
    if ([[model.actionModel.type stringValue] isEqualToString:@"1"]) {
        PosterDetailWebViewController *posterWebVC = [[PosterDetailWebViewController alloc] init];
        posterWebVC.model = model;
        [self.navigationController pushViewController:posterWebVC animated:YES];
    } else {
        PosterDetailViewController *posterVC = [[PosterDetailViewController alloc] init];
        posterVC.model = model;
        [self.navigationController pushViewController:posterVC animated:YES];
        // 写跳转PosterViewController;
//        NSLog(@"dsf");
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    }else if (indexPath.section == 2) {
        return (470 / 736.0) * HEIGHT;
    } else if (indexPath.section == 5) {
        return 500 / 736.0 * HEIGHT;
    } else if (indexPath.section == 6) {
        return 180 / 736.0 * HEIGHT;
    } else {
        return 250 / 736.0 * HEIGHT;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.musicArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NewSongNewDVDViewController *newSongVC = [[NewSongNewDVDViewController alloc] init];
        [self.navigationController pushViewController:newSongVC animated:YES];
    } else if (indexPath.section == 2) {
        HotListViewController *hotListVC = [[HotListViewController alloc] init];
        
        [self.navigationController pushViewController:hotListVC animated:YES];
    } else if (indexPath.section == 3) {
        RecommendViewController *recommendVC = [RecommendViewController new];
        [self.navigationController pushViewController:recommendVC animated:YES];
    } else if (indexPath.section == 5) {
        ExclusiveRegionViewController *exclusiveVC = [ExclusiveRegionViewController new];
        [self.navigationController pushViewController:exclusiveVC animated:YES];
    } else if (indexPath.section == 4) {
        ExclusiveArtistViewController *exclusiveVC = [ExclusiveArtistViewController new];
        [self.navigationController pushViewController:exclusiveVC animated:YES];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        RecommendSongsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendSongsTableViewCell class])];
//        cell.model = self.musicArray[indexPath.section];
//        cell.collectionView.tag = 10000 + indexPath.section;
//        cell.selectionStyle = 0;
//        cell.RecommendBlock = ^(NSInteger tag, BaseModel *model){
//            NSLog(@"%ld",tag);
//            NSLog(@"%@",model);
//        };
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    } else if (indexPath.section == 5 || indexPath.section == 6) {
        JHJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JHJTableViewCell class])];
        cell.model = self.musicArray[indexPath.section];
        cell.collectionView.tag = 10000 + indexPath.section;
        cell.selectionStyle = 0;
        cell.JHJBlock = ^(BaseModel *model, NSInteger tag) {
            DataModel *dataModel =(DataModel *)model;
            if ([[dataModel.actionModel.type stringValue] isEqualToString:@"1"]) {
//                NSLog(@"%@",dataModel.actionModel.)
                PosterDetailWebViewController *webViewVC = [PosterDetailWebViewController new];
                webViewVC.picUrl = dataModel.actionModel.value;
                [self.navigationController pushViewController:webViewVC animated:YES];
            } else {
                PosterDetailViewController *posterVC = [[PosterDetailViewController alloc] init];
                posterVC.value = [NSNumber numberWithInt: [dataModel.actionModel.value intValue]];
                [self.navigationController pushViewController:posterVC animated:YES];
            }
        };
        return cell;
    } else {
        // 新歌新碟热门歌单推荐榜单独家艺人 Cell;
        ExclusiveRegionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ExclusiveRegionTableViewCell class])];
        cell.model = self.musicArray[indexPath.section];
        cell.collectionView.tag = 10000 + indexPath.section;
        cell.selectionStyle = 0;
        cell.exclusiveBlock = ^(NSInteger tag, BaseModel *model) {
            DataModel *dataModel = (DataModel *)model;
            if (dataModel.actionModel.type == [NSNumber numberWithInteger:19]) {
                PosterDetailViewController *detailVC = [[PosterDetailViewController alloc] init];
                detailVC.value =[NSNumber numberWithInteger:[dataModel.actionModel.value integerValue]];
                [self.navigationController pushViewController:detailVC animated:YES];
            } else if (dataModel.actionModel.type == [NSNumber numberWithInteger:5]) {
                
                PosterDetailViewController *detailVC = [[PosterDetailViewController alloc] init];
                detailVC.valueStr = dataModel.actionModel.value;
                [self.navigationController pushViewController:detailVC animated:YES];
            } else if (indexPath.section == 3) {
                RecommendListViewController *recommendListVC = [RecommendListViewController new];
                recommendListVC.value = dataModel.actionModel.value;
                [self.navigationController pushViewController:recommendListVC animated:YES];
            } else if (indexPath.section == 4) {
                SongListDetailViewController *songlistVC = [SongListDetailViewController new];
                songlistVC.value = dataModel.actionModel.value;
                [self.navigationController pushViewController:songlistVC animated:YES];
            }
        };
        return cell;
    }
}

//- (void)viewWillDisappear:(BOOL)animated {
//    self.automaticallyAdjustsScrollViewInsets = NO;
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"0-----%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y > self.currentOffSit_y && scrollView.contentOffset.y <= 1394) {
        [self.tabBar hiddenTabBarWithAnimation:YES];
        self.currentOffSit_y = scrollView.contentOffset.y;
        NSLog(@"===%f",scrollView.contentSize.height);
//        NSLog(@"%f",scrollView.contentOffset.y);
        if (scrollView.contentOffset.y > scrollView.contentSize.height - 100 - HEIGHT) {
            NSLog(@"1111111");
            scrollView.bounces = NO;
        }
    } else if (scrollView.contentOffset.y < self.currentOffSit_y && scrollView.contentOffset.y > 0) {
        scrollView.bounces = YES;
        [self.tabBar showTabBarWithAnimation:YES];
        self.currentOffSit_y = scrollView.contentOffset.y;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
